import {LOGIN, ROUTES, GETUSERLIST, GETUSERINFO, UPDATEUSER, SAVEUSER, DELETEUSER } from '@/services/api'
import {request, METHOD, removeAuthorization} from '@/utils/request'

/**
 * 登录服务
 * @param account 账户名
 * @param password 账户密码
 * @returns {Promise<AxiosResponse<T>>}
 */
export async function login(account, password) {
  return request(LOGIN, METHOD.POST, {
    account: account,
    password: password
  })
}

/**
 * 退出登录
 */
export function logout() {
  localStorage.removeItem(process.env.VUE_APP_ROUTES_KEY);
  localStorage.removeItem(process.env.VUE_APP_PERMISSIONS_KEY);
  localStorage.removeItem(process.env.VUE_APP_ROLES_KEY);
  removeAuthorization()
}

/**
 * 用户列表
 * @param page 页数
 * @param limit 每页条数
 *  * @param username 角色名称
 * @returns {Promise<AxiosResponse<T>>}
 */
export async function getUserList(page,limit, username) {
  return request(GETUSERLIST, METHOD.GET, {
    page: page,
    limit: limit,
    username: username
  })
}

/**
 * 用户详情
 * @param userId 用户id
 * @returns {Promise<AxiosResponse<T>>}
 */
export async function getUserInfo(userId) {
  return request(GETUSERINFO, METHOD.GET, {
    userId: userId,
  })
}

/**
 * 修改用户
 * @param id 用户id
 * @param account 用户名称
 * @param pwd 密码
 * @param email 邮箱
 * @param phone 手机号码
 * @param status 状态
 * @param roleIdList[] 角色集合
 * @returns {Promise<AxiosResponse<T>>}
 */
export async function updateUser(id,account,pwd,email,phone,status,roleIdList) {
  return request(UPDATEUSER, METHOD.POST, {
    id: id,
    account: account,
    pwd: pwd,
    email: email,
    phone: phone,
    status: status,
    roleIdList: roleIdList,
  })
}

/**
 * 修改用户
 * @param account 用户名称
 * @param pwd 密码
 * @param email 邮箱
 * @param phone 手机号码
 * @param status 状态
 * @param roleIdList[] 角色集合
 * @returns {Promise<AxiosResponse<T>>}
 */
export async function saveUser(account,pwd,email,phone,status,roleIdList) {
  return request(SAVEUSER, METHOD.POST, {
    account: account,
    pwd: pwd,
    email: email,
    phone: phone,
    status: status,
    roleIdList: roleIdList,
  })
}
/**
 * 删除用户
 * @param id 用户id
 * @returns {Promise<AxiosResponse<T>>}
 */
export async function deleteUser(id) {
  return request(DELETEUSER, METHOD.POST, {
    ids: id
  })
}
export default {
  login,
  logout,
  getUserList,
  getUserInfo,
  saveUser,
  updateUser,
  deleteUser
}
